package gcontent.db.statistics;

import gcontent.db.*;
import gcontent.db.DBParams.Direction;
import gcontent.utils.Config;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Types;
import java.util.Calendar;

public class Statistics extends BaseDB {
	
	public Statistics(boolean autoCommit) throws IOException {
		super(Config.getConfigFile(), autoCommit);		
	}
	
	public Statistics() throws IOException {
		super(Config.getConfigFile(), true);		
	}	
	
	public void AddVisit() {
		try {
			this.connect();
			this.executeProcedure(-1, "PROC_addVisit", null);
			this.commit();
			this.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void AddSearch(String str, int type) {
		try {
			DBParams [] param_values = new DBParams[2];
			param_values[0] = new DBParams(Types.VARCHAR, Direction.d_input, str);
			param_values[1] = new DBParams(Types.INTEGER, Direction.d_input, type);
			
			this.connect();
			this.executeProcedure(-1, "PROC_addSearchString", param_values);
			this.commit();
			this.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	public DBParams [] countDaysRunning() {
		DBParams [] param_values = null;
		
		try {			
			param_values = new DBParams[1];			             
			param_values[0] = new DBParams(Types.DATE, Direction.d_output, 0);
			this.connect();
			this.executeProcedure(-1, "PROC_getFirstVisit", param_values);
			this.disconnect();
		} catch (Exception e) {
			e.printStackTrace();			
		}
		
		return param_values;
	}
	
	public DBParams [] countSearches() {
		DBParams [] param_values = null;
		
		try {			
			Calendar dateInitMonth = gcontent.utils.Dates.getFirstDayOfMonth();
			Calendar dateFinishMonth = gcontent.utils.Dates.getLastDayOfMonth();
			Calendar dateInitYear = gcontent.utils.Dates.getFirstDayOfYear();
			Calendar dateFinishYear = gcontent.utils.Dates.getLastDayOfYear();			
			
			param_values = new DBParams[14];			             
			param_values[0] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateInitMonth));
			param_values[1] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateFinishMonth));
			param_values[2] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateInitYear));
			param_values[3] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateFinishYear));
			param_values[4] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[5] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[6] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[7] = new DBParams(Types.VARCHAR, Direction.d_output, 0);
			param_values[8] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[9] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[10] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[11] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[12] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[13] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			
			this.connect();
			this.executeProcedure(-1, "PROC_getSearches", param_values);
			this.disconnect();
		} catch (Exception e) {
			e.printStackTrace();			
		}
		
		return param_values;
	}
	
	public DBParams [] countVisits() {
		DBParams [] param_values = null;
		
		try {			
			Calendar dateInitMonth = gcontent.utils.Dates.getFirstDayOfMonth();
			Calendar dateFinishMonth = gcontent.utils.Dates.getLastDayOfMonth();
			Calendar dateInitYear = gcontent.utils.Dates.getFirstDayOfYear();
			Calendar dateFinishYear = gcontent.utils.Dates.getLastDayOfYear();			
			
			param_values = new DBParams[7];			             
			param_values[0] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateInitMonth));
			param_values[1] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateFinishMonth));
			param_values[2] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateInitYear));
			param_values[3] = new DBParams(Types.DATE, Direction.d_input, gcontent.utils.Dates.getSQLDate(dateFinishYear));
			param_values[4] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[5] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			param_values[6] = new DBParams(Types.INTEGER, Direction.d_output, 0);
			
			this.connect();
			this.executeProcedure(-1, "PROC_getVisits", param_values);
			this.disconnect();
		} catch (Exception e) {
			e.printStackTrace();			
		}
		
		return param_values;
	}
}
